/*
 * Copyright (C) 2006 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.catonine.swing.rule;

import javax.swing.table.AbstractTableModel;

import nu.zoom.catonine.stylerule.StyleRule;
import nu.zoom.catonine.stylerule.StyleRuleListener;
import nu.zoom.catonine.stylerule.StyleRules;
import nu.zoom.catonine.stylerule.StyleRulesListener;
import nu.zoom.ui.Resources;

/**
 * A table model to display the styling rules in a style rules collection.
 * 
 * @author "Johan Maasing" &lt;johan@zoom.nu&gt;
 * 
 */
public class ConfigTableModel extends AbstractTableModel implements StyleRulesListener, StyleRuleListener {

	private static final long serialVersionUID = 1L;
	private Resources messages;
	private StyleRules styleRules;

	public ConfigTableModel(Resources messages, StyleRules styleRules) {
		super();
		this.messages = messages;
		this.styleRules = styleRules;
		styleRules.addListener(this);
		styleRules.addStyleRuleListener(this);
	}

	@Override
	public synchronized int getRowCount() {
		return styleRules.getPatternStyles().size();
	}

	@Override
	public int getColumnCount() {
		return 2;
	}

	public synchronized StyleRule getValueAt(int rowIndex) {
		return styleRules.getPatternStyles().get(rowIndex);
	}

	@Override
	public synchronized Object getValueAt(int rowIndex, int columnIndex) {
		StyleRule config = styleRules.getPatternStyles().get(rowIndex);
		return config;
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	public synchronized void addRow(StyleRule config) {
		styleRules.add(config);
	}

	private synchronized void firePatternChanged(StyleRule rule) {
		int row = styleRules.getPatternStyles().indexOf(rule);
		fireTableCellUpdated(row, 0);
		fireTableCellUpdated(row, 1);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.AbstractTableModel#getColumnName(int)
	 */
	@Override
	public String getColumnName(int column) {
		try {
			return messages.getMessage("nu.zoom.catonine.stylerule.table." + column);
		} catch (Resources.ResourceNotFoundException resourceNotFoundException) {
			// TODO: Add logging
			return null;
		}
	}

	@Override
	public void listChangedName(StyleRules list) {
		// We don't display the name so we can ignore this call.
	}

	@Override
	public void listDataChanged(StyleRules list) {
		// We don't display the data so we can ignore this call.
	}

	@Override
	public void listStructureChanged(StyleRules newStyleRules) {
		int oldRowCount = styleRules.getPatternStyles().size();
		int newRowCount = newStyleRules.getPatternStyles().size();
		styleRules.removeListener(this);
		styleRules = newStyleRules;
		styleRules.addListener(this);
		// Remove first since we do not want to be registered twice on old
		// rules.
		styleRules.removeStyleRuleListener(this);
		styleRules.addStyleRuleListener(this);
		// fireTableDataChanged() - Which would seem like the perfect event
		// messes up the selections in the table. TableRowsUpdated does not even
		// if we say that all rows are updated
		if (oldRowCount > newRowCount) {
			fireTableRowsDeleted(newRowCount, oldRowCount);
		} else {
			fireTableRowsInserted(oldRowCount, newRowCount);
		}
		int rowsToUpdate = Math.min(oldRowCount, newRowCount);
		fireTableRowsUpdated(0, rowsToUpdate);
	}

	@Override
	public void patternChanged(StyleRule pattern) {
		firePatternChanged(pattern);
	}
}
